package org.jsola.hr.web;

import com.alibaba.excel.util.CollectionUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.*;
import org.jsola.hr.query.CompanyAdditionalFieldsQuery;
import org.jsola.hr.service.ICompanyAdditionalFieldsService;
import org.jsola.hr.service.ICustomTemplateService;
import org.jsola.hr.service.IEmpInfoService;
import org.jsola.hr.service.async.IAsyncEmpInfoDetailService;
import org.jsola.hr.vo.CompanyAdditionalFieldsListVO;
import org.jsola.hr.vo.CompanyAdditionalFieldsVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 公司员工额外字段表
 *
 * @author zhr
 */
@RestController("hrCompanyAdditionalFieldsController")
@RequestMapping(HrConstants.API_PATH + "/v1/companyAdditionalFields")
public class CompanyAdditionalFieldsController implements ICompanyAdditionalFieldsControllerAPI {

    @Autowired
    private ICompanyAdditionalFieldsService companyAdditionalFieldsService;

    @Autowired
    private ICustomTemplateService customTemplateService;

    @Autowired
    private IEmpInfoService empInfoService;

    @Autowired
    private IAsyncEmpInfoDetailService asyncEmpInfoDetailService;

    @Override
    @GetMapping("/querySalaryHead/{companyId}")
    public Result<Integer> querySalaryHead(@PathVariable String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增公司员工额外字段表权限校验

        return Result.success(
                companyAdditionalFieldsService.querySalaryHead(companyId,tokenUser)
        );
    }

    @Override
    @PostMapping("/updateHead")
    public Result<Integer> updateHead(@Valid @RequestBody CompanyAdditionalFieldsQuery companyAdditionalFieldsQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增公司员工额外字段表权限校验

        return Result.success(
                companyAdditionalFieldsService.updateHead(companyAdditionalFieldsQuery, tokenUser)
        );
    }

    @Override
    @GetMapping("/getHead")
    public Result<CompanyAdditionalFieldsListVO> getHead(@Valid CompanyAdditionalFieldsQuery companyAdditionalFieldsQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增公司员工额外字段表权限校验

        return Result.success(
                companyAdditionalFieldsService.getHead(companyAdditionalFieldsQuery, tokenUser)
        );
    }

    @Override
    @PostMapping
    public Result<CompanyAdditionalFieldsVO> save(@Valid @RequestBody CompanyAdditionalFieldsAddDTO companyAdditionalFieldsAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增公司员工额外字段表权限校验

        return Result.success(
                companyAdditionalFieldsService.save(companyAdditionalFieldsAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping("save2")
    public Result<CompanyAdditionalFieldsVO> save2(@Valid @RequestBody CompanyAdditionalFieldsAddDTO companyAdditionalFieldsAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增公司员工额外字段表权限校验

        return Result.success(
                companyAdditionalFieldsService.save2(companyAdditionalFieldsAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping("/saveForCustom")
    public Result<CompanyAdditionalFieldsVO> saveForCustom(@Valid @RequestBody CompanyAdditionalFieldsAddDTO companyAdditionalFieldsAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增公司员工额外字段表权限校验

        return Result.success(
                companyAdditionalFieldsService.saveForCustom(companyAdditionalFieldsAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CompanyAdditionalFieldsUpdateDTO companyAdditionalFieldsUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改公司员工额外字段表权限校验

        int count = companyAdditionalFieldsService.update(companyAdditionalFieldsUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @PutMapping("/update2")
    public Result<Integer> update2(@Valid @RequestBody CompanyAdditionalFieldsUpdateDTO companyAdditionalFieldsUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改公司员工额外字段表权限校验

        int count = companyAdditionalFieldsService.update2(companyAdditionalFieldsUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @PutMapping("/openField")
    public Result<Integer> openField(@Valid @RequestBody OpenFieldUpdateDTO openFieldUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改公司员工额外字段表权限校验

        int count = companyAdditionalFieldsService.openField(openFieldUpdateDTO.getCompanyAdditionalFieldsId()
                , openFieldUpdateDTO.getIsOpen(), tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @PutMapping("/necessaryField")
    public Result<Integer> necessaryField(@Valid @RequestBody NecessaryFieldUpdateDTO necessaryFieldUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改公司员工额外字段表权限校验

        int count = companyAdditionalFieldsService.necessaryField(necessaryFieldUpdateDTO.getCompanyAdditionalFieldsId()
                , necessaryFieldUpdateDTO.getIsNecessary(), tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }

        //更新员工的 “信息补充 未完善、已完善”
        List<String> list = empInfoService.listEmpIdByCompany(necessaryFieldUpdateDTO.getCompanyId(), tokenUser.getSiteId());
        asyncEmpInfoDetailService.empCompleteInfoAsync(list, tokenUser);

        return Result.success(count);
    }

    @Override
    @PutMapping("/updateForCustom")
    public Result<Integer> updateForCustom(@Valid @RequestBody CompanyAdditionalFieldsUpdateDTO companyAdditionalFieldsUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改公司员工额外字段表权限校验

        int count = companyAdditionalFieldsService.updateForCustom(companyAdditionalFieldsUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody CompanyAdditionalFieldsBatchDTO companyAdditionalFieldsBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<CompanyAdditionalFieldsAddDTO> companyAdditionalFieldsAddDTOList = companyAdditionalFieldsBatchDTO.getCompanyAdditionalFieldsAddDTOList();
        if (!CollectionUtils.isEmpty(companyAdditionalFieldsAddDTOList)) {
            companyAdditionalFieldsService.batchSave(companyAdditionalFieldsAddDTOList,tokenUser);
        }
        // 修改
        List<CompanyAdditionalFieldsUpdateDTO> companyAdditionalFieldsUpdateDTOList = companyAdditionalFieldsBatchDTO.getCompanyAdditionalFieldsUpdateDTOList();
        if (!CollectionUtils.isEmpty(companyAdditionalFieldsUpdateDTOList)) {
            companyAdditionalFieldsUpdateDTOList.parallelStream().forEach(
                    companyAdditionalFieldsUpdateDTO -> {
                        companyAdditionalFieldsService.update(companyAdditionalFieldsUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<Long> deleteCompanyAdditionalFieldsIdList = companyAdditionalFieldsBatchDTO.getDeleteCompanyAdditionalFieldsIdList();
        if (!CollectionUtils.isEmpty(deleteCompanyAdditionalFieldsIdList)) {
            companyAdditionalFieldsService.deleteByIds(tokenUser,deleteCompanyAdditionalFieldsIdList.toArray(new Long[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{companyAdditionalFieldsId}")
    public Result<Integer> delete(@PathVariable Long companyAdditionalFieldsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除公司员工额外字段表权限校验

        int count = companyAdditionalFieldsService.deleteByIds(tokenUser, companyAdditionalFieldsId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping(value = "/deleteForCustom/{companyAdditionalFieldsId}")
    public Result<Integer> deleteForCustom(@PathVariable Long companyAdditionalFieldsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除公司员工额外字段表权限校验

        int count = companyAdditionalFieldsService.deleteForCustom(tokenUser, companyAdditionalFieldsId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping(value = "/deleteForPc/{companyAdditionalFieldsId}")
    public Result<Integer> deleteForPc(@PathVariable Long companyAdditionalFieldsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除公司员工额外字段表权限校验

        int count = companyAdditionalFieldsService.deleteForPc(tokenUser, companyAdditionalFieldsId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping(value = "/deleteForPc2/{companyAdditionalFieldsId}")
    public Result<Integer> deleteForPc2(@PathVariable Long companyAdditionalFieldsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除公司员工额外字段表权限校验

        int count = companyAdditionalFieldsService.deleteForPc2(tokenUser, companyAdditionalFieldsId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] companyAdditionalFieldsIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除公司员工额外字段表权限校验

        int count = companyAdditionalFieldsService.deleteByIds(tokenUser, companyAdditionalFieldsIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{companyAdditionalFieldsId}")
    public Result<CompanyAdditionalFieldsVO> findById(@PathVariable Long companyAdditionalFieldsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司员工额外字段表权限校验

        return Result.success(
                companyAdditionalFieldsService.selectById(companyAdditionalFieldsId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CompanyAdditionalFieldsListVO>> listAll(@Valid CompanyAdditionalFieldsQuery companyAdditionalFieldsQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司员工额外字段表权限校验

        return Result.success(
                companyAdditionalFieldsService.select(companyAdditionalFieldsQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listByCompanyId")
    public Result<List<CompanyAdditionalFieldsListVO>> listByCompanyId(@RequestParam("moduleCode") String moduleCode,
                                                                       @RequestParam("companyId") String companyId,
                                                                       String templateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司员工额外字段表权限校验

        return Result.success(
                companyAdditionalFieldsService.listByCompanyId(moduleCode, companyId, templateId, tokenUser)
        );
    }


    @Override
    @GetMapping
    public Result<Page<CompanyAdditionalFieldsListVO>> page(@Valid CompanyAdditionalFieldsQuery companyAdditionalFieldsQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companyAdditionalFieldsService.selectPage(companyAdditionalFieldsQuery, tokenUser.getSiteId())
        );
    }

    /**
     * 修复预登记的自定义字段 companyId=site,moduleType=checkIn,templateId=list
     */
    @PostMapping("/repairCheckIn")
    public Result repairCheckIn() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        customTemplateService.repairCheckIn();
        return Result.success();
    }

    /**
     * 修复租户级花名册基本字段 从系统级中创建
     */
    @PostMapping("/repairNecessary")
    public Result repairNecessary() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        customTemplateService.repairNecessary();
        return Result.success();
    }

    /**
     * 修复租户级自定义字段
     */
    @PostMapping("/repairSiteAdditionalFields")
    public Result repairSiteAdditionalFields(String siteId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        customTemplateService.repairSiteAdditionalFields(siteId);
        return Result.success();
    }


}

